import sys
import wx

from LMG.Utility.constants import *

try:
    import winxpgui as win32gui
    from struct import pack, unpack, calcsize
    from array import array 
    WIN32 = True 
except:
    WIN32 = False

class MenuBar(wx.MenuBar):
    """
    Handles the menus at the top of the window
    """
    def __init__(self, parent):
        self.parent = parent
        
        style = wx.CLIP_CHILDREN
        wx.MenuBar.__init__(self, style = style)
        
        self.updateMenu()
                    
    def updateMenu(self):
        for item in range(self.GetMenuCount()):
            menu = self.Remove(0)
            menu.Destroy()
        
        items = [ACTION_MENUFILE,
                 ACTION_MENUEDIT,
                 ACTION_MENUVIEW,
                 ACTION_MENUHELP]
        for item in items:
            utility.actions[item].addToMenu(self, bindto = self.parent)

    def ChangeMenuBarColor(self):
        """
        Changes the background color of the menubar and optionally gives 
        different colors to menu items
        """
        if not WIN32:
            return

        self.hwnd = utility.frame.GetHandle()
        # Set color
        c = utility.actions[ACTION_THEME].colour
        if c:
            hbrBack = win32gui.CreateSolidBrush(c.GetRGB())
        else:
            hbrBack = win32gui.GetSysColorBrush(15)
        # MENUINFO
        item = pack("lL5l", calcsize("lL5l"), 0x00000002|0x80000000, 0, 0, hbrBack, 0, 0)
        menuinfo = array("c", item)
        # Change menu
        win32gui.SetMenuInfo(win32gui.GetMenu(self.hwnd), menuinfo)
        win32gui.DrawMenuBar(self.hwnd)
        
